

package vistas;

import com.toedter.calendar.JDateChooser;
import controladores.COrden;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import modelos.MTabla;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VOrden extends javax.swing.JFrame {

    private Vector<JTextField> edit;
    private Vector<JButton> boton;
    private COrden control;
    private MTabla modeloTabla;
    private Vector<String> fila;
    private AuxVista auxvista;

    public VOrden() {
        initComponents();
        modeloTabla= new MTabla();
        auxvista = new AuxVista();
        fila= new Vector<String>();
        edit = new Vector<JTextField>();
        boton = new Vector<JButton>();
        control= new COrden(this);

        tblProdcuto.setModel(modeloTabla);
        fila.add("Producto");
        fila.addElement("Precio");
        fila.add("Cantidad");
        fila.add("Sub-Total");
        modeloTabla.setColumnIdentifiers(fila);

        boton.add(btnSalir);
        boton.add(btnConsultar);
        boton.add(btnRegistrar);
        boton.add(btnCancelar);
        boton.add(btnEliminar);
        boton.add(btnAgregar);
        boton.add(btnQuitar);

        edit.add(txtCodigo);
        edit.add(txtNombre);
        edit.add(txtCedula);
        edit.add(txtTotal);       
        
        for (int i = 0; i < boton.size(); i++)
            boton.elementAt(i).addActionListener(control);
        reiniciar();
        txtTotal.setEditable(false);
        cargarComboCantidad();
    }

    public void bloquear(){
        txtCodigo.setEditable(false);
        txtNombre.setEditable(false);
        txtCedula.setEditable(false);
        btnAgregar.setEnabled(false);
        btnQuitar.setEnabled(false);
    }

    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbProducto.getItemAt(cmbProducto.getSelectedIndex()))){
                int c = Integer.parseInt(""+cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()));
                double sub = Double.parseDouble(""+modeloTabla.getValueAt(i, 1))*c;
                modeloTabla.setValueAt(""+sub, i, 3);
                return true;
            }
        return false;
    }

    public boolean validarCamposVacios(){
        return auxvista.validarCamposVacios(edit);
    }

    public boolean validarNumeros(){
        return auxvista.validarNumeros(edit);
    }

    public boolean validarFecha(){
        return auxvista.validarFecha(dtdFecha.getDate());
    }


    public void agregarFila(String[] v){
        float total;
        if(!txtTotal.getText().isEmpty())
            total = Float.parseFloat(txtTotal.getText());
        else
            total=0;
        if(!actualizar()){
            int c = Integer.parseInt(""+cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()));
            fila= new Vector<String>();
            fila.add(v[0]);
            fila.add(v[1]);
            fila.add(""+c);
            float sub = (Float.parseFloat(v[1])*c);
            fila.add("" + sub);
            total+=sub;
            txtTotal.setText(""+total);
            modeloTabla.addRow(fila);
        }
    }

     public void agregarFila1(String [] v){
         int c = Integer.parseInt(""+cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()));
         fila= new Vector<String>();
         fila.add(v[0]);
         fila.add(v[1]);
         fila.add(""+c);
         float sub = (Float.parseFloat(v[1])*c);
         fila.add("" + sub);
         modeloTabla.addRow(fila);
     }
     

    public void calcularTotal(){
        float total =0;
        for (int i = 0; i < modeloTabla.getRowCount(); i++) 
            total+=Float.parseFloat(""+modeloTabla.getValueAt(i, 3));
        txtTotal.setText(""+total);
    }

    public void quitar(){
        int i = tblProdcuto.getSelectedRow();
        if(i != -1){
            float t = Float.parseFloat(txtTotal.getText())-Float.parseFloat((String) modeloTabla.getValueAt(i, 3));
            modeloTabla.removeRow(i);
            txtTotal.setText(""+t);
        }

        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Comida Rapida",JOptionPane.ERROR_MESSAGE);
    }

     public void reiniciar(){
         dtdFecha.setDate(new Date());
        auxvista.reiniciar(edit);
        txtCodigo.setEditable(true);
        btnAgregar.setEnabled(true);
        btnQuitar.setEnabled(true);
        txtNombre.setEditable(true);
        txtCedula.setEditable(true);
        modeloTabla.setRowCount(0);
     }

    public void cargarComboProductos(Vector<String> descripciones){
        cmbProducto.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbProducto.addItem(descripciones.elementAt(i));

        }
    }

    public void cargarComboCantidad(){
        for (int i = 1; i <= 100; i++)
            cmbCantidad.addItem(i);
    }

    public Vector<JTextField> getTextos() {
        return edit;
    }

    public JTextField getTxtCedula() {
        return txtCedula;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtNombre() {
        return txtNombre;
    }

    public JTextField getTxtTotal() {
        return txtTotal;
    }

    public JDateChooser getDtdFecha() {
        return dtdFecha;
    }

    public int getCantidad() {
        return (Integer)cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex());
    }

    public String getTextoCmbProducto() {
        return (String) cmbProducto.getItemAt(cmbProducto.getSelectedIndex());
    }

    public Vector<String []> getInfoTabla(){
          Vector<String []> v = new Vector<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = new String [] {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 2)};
            v.add(s);
         }
         return v;
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtTotal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProdcuto = new javax.swing.JTable();
        dtdFecha = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtCedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cmbProducto = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cmbCantidad = new javax.swing.JComboBox();
        btnAgregar = new javax.swing.JButton("Agregar");
        btnQuitar = new javax.swing.JButton("Quitar");
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnRegistrar = new javax.swing.JButton("Registrar");
        btnConsultar = new javax.swing.JButton("Consultar");
        btnCancelar = new javax.swing.JButton("Cancelar");
        btnSalir = new javax.swing.JButton("Salir");
        btnEliminar = new javax.swing.JButton("Eliminar");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Maestro de Ordenes");
        getContentPane().setLayout(null);

        txtTotal.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtTotal);
        txtTotal.setBounds(80, 400, 110, 30);

        jLabel5.setText("Total:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(20, 400, 55, 30);

        tblProdcuto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProdcuto);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(12, 220, 668, 160);
        jScrollPane1.setBackground(new java.awt.Color(255,255,255));
        getContentPane().add(dtdFecha);
        dtdFecha.setBounds(560, 0, 122, 28);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(null);

        jLabel6.setText("Codigo:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(20, 10, 100, 30);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        jPanel1.add(txtCodigo);
        txtCodigo.setBounds(100, 10, 130, 30);

        jLabel2.setText("Nombre:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 90, 140, 32);

        txtNombre.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtNombre);
        txtNombre.setBounds(100, 90, 130, 32);

        txtCedula.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtCedula);
        txtCedula.setBounds(100, 50, 130, 30);

        jLabel3.setText("Cedula:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 50, 100, 32);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 60, 260, 140);
        jPanel1.setBackground(new java.awt.Color(255,255,255));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(null);

        jPanel2.add(cmbProducto);
        cmbProducto.setBounds(10, 30, 190, 30);

        jLabel7.setText("Cantidad:");
        jPanel2.add(jLabel7);
        jLabel7.setBounds(220, 30, 80, 30);

        cmbCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbCantidadActionPerformed(evt);
            }
        });
        jPanel2.add(cmbCantidad);
        cmbCantidad.setBounds(300, 30, 82, 30);

        btnQuitar.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        btnQuitar.setText("Quitar");
        jPanel2.add(btnQuitar);
        btnQuitar.setBounds(230, 80, 120, 30);

        btnAgregar.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        btnAgregar.setText("Agregar");
        jPanel2.add(btnAgregar);
        btnAgregar.setBounds(70, 80, 120, 30);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(280, 60, 400, 140);
        jPanel2.setBackground(new java.awt.Color(255,255,255));

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel4.setText("Productos");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(349, 20, 149, 30);

        jLabel8.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel8.setText("Orden");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(100, 20, 80, 30);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(null);

        btnRegistrar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnRegistrar);
        btnRegistrar.setBounds(30, 20, 120, 30);

        btnConsultar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnConsultar.setText("Consultar");
        jPanel3.add(btnConsultar);
        btnConsultar.setBounds(160, 20, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnCancelar);
        btnCancelar.setBounds(30, 100, 120, 30);

        btnSalir.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnSalir.setText("Salir");
        jPanel3.add(btnSalir);
        btnSalir.setBounds(160, 100, 120, 30);

        btnEliminar.setFont(new java.awt.Font("Ubuntu", 1, 15));
        btnEliminar.setText("Eliminar");
        jPanel3.add(btnEliminar);
        btnEliminar.setBounds(100, 60, 120, 30);

        getContentPane().add(jPanel3);
        jPanel3.setBounds(210, 400, 310, 150);
        jPanel3.setBackground(new java.awt.Color(255,255,255));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 694, 602);
        getContentPane().setBackground(new java.awt.Color(14,151,255));
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

    private void cmbCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbCantidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbCantidadActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VOrden().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCantidad;
    private javax.swing.JComboBox cmbProducto;
    private com.toedter.calendar.JDateChooser dtdFecha;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblProdcuto;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    public String [] getRegistro() {
          String [] s = new String [] {""+cmbProducto.getItemAt(cmbProducto.getSelectedIndex()),""+cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex())};

         return s;
    }

	public javax.swing.JComboBox getCmbCantidad() {
		return cmbCantidad;
	}

	public void setCmbCantidad(javax.swing.JComboBox cmbCantidad) {
		this.cmbCantidad = cmbCantidad;
		cmbCantidad.setBackground(new java.awt.Color(14,151,255));
	}

	public javax.swing.JComboBox getCmbProducto() {
		return cmbProducto;
	}

	public void setCmbProducto(javax.swing.JComboBox cmbProducto) {
		this.cmbProducto = cmbProducto;
		cmbProducto.setBackground(new java.awt.Color(14,151,255));
	}
    
    



}
